<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'cash';
$return=array();
//代金券
if($_RQ['op']=='cash'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid'],'card_type'=>'CASH');
    $orderby='id desc';
	$total='';
	
	$return['list']=pdo_getslice('jy_weishop_wxcard',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$return['type']=array('CASH'=>'代金券','DISCOUNT'=>'折扣券');
	
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'cash','title'=>'代金券'),
			array('op'=>'discount','title'=>'折扣券'),
	
		),
		'list'=>array('data'=>'list','tip'=>'【温馨提示】：如果创<red>建卡券失败</red>，请移步：微信公众号官方--》开启卡券功能--》设置好卡券相关细节（门店等）','edit_op'=>'editcash','list'=>array(
			array('name'=>'title','title'=>'卡券名称','type'=>'string'),
			array('name'=>'card_type','title'=>'卡券类型','type'=>'radio','options'=>'type'),
			array('name'=>'total_quantity','title'=>'总库存','type'=>'string'),
			array('name'=>'quantity','title'=>'当前库存','type'=>'string'),				
		)),
		'search_one'=>array('name'=>'title','add_op'=>'addcash'),
	);
	$return['htmls']=$htmls;
	
	exi($return);
}
if($_RQ['op']=='addcash'){
	$id=intval($_RQ['id']);
	if($id){
	$item=pdo_get('jy_weishop_wxcard',array('id'=>$id));
	}else{
		$item=array();
	}
    $return['item']=$item;
	$return['group']=pdo_getall('jy_weishop_coupon_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['isdescsys']=array('0'=>'不使用','1'=>'使用');
	$return['gettype']=array('1'=>'显示','0'=>'不显示');
	$return['limitdiscounttype']=array('0'=>'无优惠使用限制','1'=>'不可与促销优惠同时使用','2'=>'不可与会员折扣同时使用','3'=>'不可与促销优惠和会员折扣同时使用');
	$return['limitgoodtype']=array('0'=>'不添加商品限制','1'=>'允许以下商品使用');
	$return['limitgoodcatetype']=array('0'=>'不添加商品分类限制','1'=>'允许以下商品分类使用');
	$return['coupontype']=array('0'=>'返余额','1'=>'返积分','2'=>'返现金');
	$return['timelimittype']=array('0'=>'获得多少天内','1'=>'时间段内');
	$htmls[]=array(
		'type'=>'look',
		'width'=>'350px',
		'flex'=>1,
		'tabs'=>array(
			array('op'=>'cash','title'=>'代金券'),
			array('op'=>'discount','title'=>'折扣券'),
		),
		'look'=>array(
				array('type'=>'wxcard'),
			),
	);
	$htmls[]=array(
		'type'=>'edit',
		'width'=>'820px',
		'post'=>'post1',
		'tabs'=>array(
			array('op'=>'cash','title'=>'代金券'),
			array('op'=>'discount','title'=>'折扣券'),
		),
		'desc'=>'优惠券设置',
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本设置',
				'list'=>array(
					array('type'=>'string','name'=>'displayorder','title'=>'排序','help'=>'越大越靠前'),
					array('type'=>'string','name'=>'title','title'=>'卡券名称','help'=>'卡券标题不能超过9个字符,此参数一旦创建则无法修改'),
					array('type'=>'select','name'=>'catid','title'=>'分类','options'=>'group'),
					array('type'=>'image','name'=>'logo_url','title'=>'logo图片','help'=>'卡券的商户logo，建议像素为300*300。图片大小不能超过1mb,仅支持jpg,png格式'),
					array('type'=>'string','name'=>'brand_name','title'=>'商铺名称','help'=>'商铺名称限制在12个字符之内,此参数一旦创建则无法修改'),
					
					array('type'=>'string','name'=>'total_quantity','title'=>'库存总量','help'=>'总共发布的库存总量，必须大于1'),
					array('type'=>'image','name'=>'abstractimglocalpath','title'=>'封面图片','help'=>'图片建议尺寸：850像素*350像素，大小不超过2M。'),
					array('type'=>'string','name'=>'abstract','title'=>'封面简介','help'=>'封面一旦创建完成则不允许修改,请注意!'),
					array('type'=>'string','name'=>'least_cost','title'=>'使用条件','help'=>'消费满多少可用, 空或0 不限制,此参数一旦创建则无法修改'),
					array('type'=>'string','name'=>'reduce_cost','title'=>'减免金额','help'=>'可抵扣的金额,此参数一旦创建则无法修改'),
					array('type'=>'radio','name'=>'datetype','title'=>'使用时间限制','options'=>'timelimittype'),
					array('type'=>'string','name'=>'fixed_term','title'=>'在领取后天数内有效','show'=>array('datetype'=>0)),
					array('type'=>'time_range','name'=>'timestamp','title'=>'在时间段内有效','show'=>array('datetype'=>1)),	
					array('type'=>'string','name'=>'get_limit','title'=>'领券限制','help'=>'每个用户领券上限，必须大于1如不填，则默认为1'),
					array('type'=>'string','name'=>'use_limit','title'=>'使用限制','help'=>'每个用户可以使用此卡券的次数,必须大于1如不填，则默认为1'),
					array('type'=>'string','name'=>'accept_category','title'=>'适用商品','help'=>'填写本券适用的商品、类目或服务,不能超过15个汉字或英文字母(仅在卡券显示用),适用与不适用商品至少填写一项'),
					array('type'=>'string','name'=>'reject_category','title'=>'不适用商品','help'=>'填写本券不适用的商品、类目或服务,不能超过15个汉字或英文字母(仅在卡券显示用),适用与不适用商品至少填写一项'),
					array('type'=>'text','name'=>'description','title'=>'使用须知'),
				)
			),
			array(
				'title'=>'自定义元素',
				'list'=>array(
					array('type'=>'switch','name'=>'setcustom','title'=>'是否设置自定义入口'),
					array('type'=>'string','name'=>'center_title','title'=>'卡券顶部按钮文字','help'=>'例如：立即使用(不能超过5个字符)'),
					array('type'=>'string','name'=>'center_sub_title','title'=>'卡券顶部按钮引导语','help'=>'(不能超过6个字符)'),
					array('type'=>'string','name'=>'center_url','title'=>'卡券顶部按钮链接','help'=>'仅卡券被用户领取且处于有效状态时显示（未到有效期、转赠中、核销后不显示）。'),
					array('type'=>'string','name'=>'custom_url_name','title'=>'入口名称'),
					array('type'=>'string','name'=>'custom_url_sub_title','title'=>'引导文字'),
					array('type'=>'link','name'=>'custom_url','title'=>'入口名称'),
				)
			),
			array(
				'title'=>'领取设置',
				'list'=>array(
					array('type'=>'radio','name'=>'gettype','options'=>'gettype','title'=>'加入领券中心','help'=>'越大越靠前'),
							
				)
			),
			array(
				'title'=>'使用限制',
				'list'=>array(
					array('type'=>'radio','name'=>'limitdiscounttype','options'=>'limitdiscounttype','title'=>'优惠使用限制'),
					array('type'=>'radio','name'=>'limitgoodcatetype','options'=>'limitgoodcatetype','title'=>'商品分类使用限制'),	
					array('type'=>'checkbox','name'=>'limitgoodcateids','title'=>'选择商品分类','show'=>array('limitgoodcatetype'=>1)),
					array('type'=>'radio','name'=>'limitgoodtype','options'=>'limitgoodtype','title'=>'商品使用限制'),
					array('type'=>'checkbox','name'=>'limitgoodids','title'=>'选择商品','show'=>array('limitgoodtype'=>1)),
								
				)
			),
	)
	);
	$return['htmls']=$htmls;
	exi($return);
	}
if($_RQ['op']=='editcash'){
	$id=intval($_RQ['id']);
	if($id){
	$item=pdo_get('jy_weishop_wxcard',array('id'=>$id,'card_type'=>'CASH'));
	$item['timestamp']=array('tstart'=>$item['begin_timestamp'],'tend'=>$item['end_timestamp']);
	}else{
		$item=array();
	}
    $return['item']=$item;
	$return['group']=pdo_getall('jy_weishop_coupon_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	
	$return['isdescsys']=array('0'=>'不使用','1'=>'使用');
	$return['gettype']=array('1'=>'显示','0'=>'不显示');
	$return['limitdiscounttype']=array('0'=>'无优惠使用限制','1'=>'不可与促销优惠同时使用','2'=>'不可与会员折扣同时使用','3'=>'不可与促销优惠和会员折扣同时使用');
	$return['limitgoodtype']=array('0'=>'不添加商品限制','1'=>'允许以下商品使用');
	$return['limitgoodcatetype']=array('0'=>'不添加商品分类限制','1'=>'允许以下商品分类使用');
	$return['coupontype']=array('0'=>'返余额','1'=>'返积分','2'=>'返现金');
	$return['timelimittype']=array('0'=>'获得多少天内','1'=>'时间段内');
	$htmls[]=array(
		'type'=>'look',
		'flex'=>1,
		'width'=>'350px',
		'tabs'=>array(
			array('op'=>'cash','title'=>'代金券'),
			array('op'=>'discount','title'=>'折扣券'),
		),
		'look'=>array(
				array('type'=>'wxcard'),
			),
	);
	$htmls[]=array(
		'type'=>'edit',
		'post'=>'cashpost',
		'tabs'=>array(
			array('op'=>'cash','title'=>'代金券'),
			array('op'=>'discount','title'=>'折扣券'),
		),
		'desc'=>'优惠券设置',
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本设置',
				'list'=>array(
					array('type'=>'string','name'=>'displayorder','title'=>'排序','help'=>'越大越靠前'),
					array('type'=>'string','name'=>'title','title'=>'卡券名称','help'=>'卡券标题不能超过9个字符,此参数一旦创建则无法修改','read'=>true),
					array('type'=>'select','name'=>'catid','title'=>'分类','options'=>'group'),
					array('type'=>'image','name'=>'logo_url','title'=>'logo图片','help'=>'卡券的商户logo，建议像素为300*300。图片大小不能超过1mb,仅支持jpg,png格式'),
					array('type'=>'string','name'=>'brand_name','title'=>'商铺名称','help'=>'商铺名称限制在12个字符之内,此参数一旦创建则无法修改','read'=>true),
					
					array('type'=>'string','name'=>'total_quantity','title'=>'库存总量','help'=>'总共发布的库存总量，必须大于1','read'=>true),
					array('type'=>'image','name'=>'abstractimglocalpath','title'=>'封面图片','help'=>'图片建议尺寸：850像素*350像素，大小不超过2M。','read'=>true),
					array('type'=>'string','name'=>'abstract','title'=>'封面简介','help'=>'封面一旦创建完成则不允许修改,请注意!','read'=>true),
					array('type'=>'string','name'=>'least_cost','title'=>'使用条件','help'=>'消费满多少可用, 空或0 不限制,此参数一旦创建则无法修改','read'=>true),
					array('type'=>'string','name'=>'reduce_cost','title'=>'减免金额','help'=>'可抵扣的金额,此参数一旦创建则无法修改','read'=>true),
					array('type'=>'radio','name'=>'datetype','title'=>'使用时间限制','options'=>'timelimittype'),
					array('type'=>'string','name'=>'fixed_term','title'=>'在领取后天数内有效','show'=>array('datetype'=>0)),
					array('type'=>'time_range','name'=>'timestamp','title'=>'在时间段内有效','show'=>array('datetype'=>1)),	
					array('type'=>'string','name'=>'get_limit','title'=>'领券限制','help'=>'每个用户领券上限，必须大于1如不填，则默认为1'),
					array('type'=>'string','name'=>'use_limit','title'=>'使用限制','help'=>'每个用户可以使用此卡券的次数,必须大于1如不填，则默认为1'),
					array('type'=>'string','name'=>'accept_category','title'=>'适用商品','help'=>'填写本券适用的商品、类目或服务,不能超过15个汉字或英文字母(仅在卡券显示用),适用与不适用商品至少填写一项'),
					array('type'=>'string','name'=>'reject_category','title'=>'不适用商品','help'=>'填写本券不适用的商品、类目或服务,不能超过15个汉字或英文字母(仅在卡券显示用),适用与不适用商品至少填写一项'),
					array('type'=>'text','name'=>'description','title'=>'使用须知'),
				)
			),
			array(
				'title'=>'自定义元素',
				'list'=>array(
					array('type'=>'switch','name'=>'setcustom','title'=>'是否设置自定义入口'),
					array('type'=>'string','name'=>'center_title','title'=>'卡券顶部按钮文字','help'=>'例如：立即使用(不能超过5个字符)'),
					array('type'=>'string','name'=>'center_sub_title','title'=>'卡券顶部按钮引导语','help'=>'(不能超过6个字符)'),
					array('type'=>'string','name'=>'center_url','title'=>'卡券顶部按钮链接','help'=>'仅卡券被用户领取且处于有效状态时显示（未到有效期、转赠中、核销后不显示）。'),
					array('type'=>'string','name'=>'custom_url_name','title'=>'入口名称'),
					array('type'=>'string','name'=>'custom_url_sub_title','title'=>'引导文字'),
					array('type'=>'link','name'=>'custom_url','title'=>'入口名称'),
				)
			),
			array(
				'title'=>'领取设置',
				'list'=>array(
					array('type'=>'radio','name'=>'gettype','options'=>'gettype','title'=>'加入领券中心','help'=>'越大越靠前'),
							
				)
			),
			array(
				'title'=>'使用限制',
				'list'=>array(
					array('type'=>'radio','name'=>'limitdiscounttype','options'=>'limitdiscounttype','title'=>'优惠使用限制'),
					array('type'=>'radio','name'=>'limitgoodcatetype','options'=>'limitgoodcatetype','title'=>'商品分类使用限制'),	
					array('type'=>'checkbox','name'=>'limitgoodcateids','title'=>'选择商品分类','show'=>array('limitgoodcatetype'=>1)),
					array('type'=>'radio','name'=>'limitgoodtype','options'=>'limitgoodtype','title'=>'商品使用限制'),
					array('type'=>'checkbox','name'=>'limitgoodids','title'=>'选择商品','show'=>array('limitgoodtype'=>1)),
								
				)
			),
	)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='cashpost'){
			$postdata=get_postdata();
			$id=intval($postdata['id']);
			
			$item=pdo_get('jy_weishop_wxcard',array('id'=>$id));
			$data = array();
			$data['card_id']=$item['card_id'];
			$data['catid'] = intval($postdata['catid']);
			$data['displayorder'] = intval($postdata['displayorder']);
			$data['card_type']='CASH';
			$data['description'] = $postdata['description'];
			$data['use_limit'] = intval($postdata['use_limit']) <= 1 ? 1 : intval($postdata['use_limit']);
			$data['get_limit'] = intval($postdata['get_limit']) <= 1 ? 1 : intval($postdata['get_limit']);
			$data['accept_category'] = $postdata['accept_category'];
			$data['reject_category'] = $postdata['reject_category'];
			$data['center_title'] = $postdata['center_title'];
			$data['center_sub_title'] = $postdata['center_sub_title'];
			$data['center_url'] = $postdata['center_url'];
			$data['gettype'] =$postdata['gettype'];
			$limitgoodcatetype = intval($postdata['limitgoodcatetype']);
			$limitgoodtype = intval($postdata['limitgoodtype']);
			$data['limitgoodcatetype'] = $limitgoodcatetype;
			$data['limitgoodtype'] = $limitgoodtype;
			$data['limitgoodcateids'] = $postdata['limitgoodcateids'];
			$data['limitgoodids'] =$postdata['limitgoodids'];
			$data['limitdiscounttype'] = $postdata['limitdiscounttype'];
					$imgurl = $postdata['logo_url'];
					$result = $this->wxcard__updateimg($imgurl);
					 if($result['errmsg'] && $result['errmsg']!='ok') {
						exi('上传的logo图片限制文件大小限制1MB，像素为300*300，仅支持JPG、PNG格式。','error');
					} 

					$data['logo_url'] = $postdata['logo_url'];
					$data['wxlogourl'] = $result['url'];
				

				if ($postdata['datetype'] == '1') {
					$data['datetype'] = $postdata['datetype'];
					$begin_timestamp = $postdata['timestamp']['tstart'];
					$end_timestamp = $postdata['timestamp']['tend'];
					
					$data['begin_timestamp'] = $begin_timestamp;
					$data['end_timestamp'] = $end_timestamp;
				}

				
				$result = $this->wxcard__updatecard($data);	
				if($result['errmsg'] && $result['errmsg']!='ok') {
						exi('卡券更新失败','error');
				} 
				pdo_update('jy_weishop_wxcard', $data, array('id' => $id, 'uniacid' => $_SESSION['uniacid']));
				exi('卡券更新成功','','cash');
			
			
}

if($_RQ['op']=='post1'){
			$id=intval($_RQ['id']);
			if($id){
			$item=pdo_get('jy_weishop_wxcard',array('id'=>$id));
			}else{
				$item=array();
			}
			$postdata=get_postdata();
			//print_r($postdata);exit();
			$data = array();
			$data['uniacid'] = $_SESSION['uniacid'];
			$data['catid'] = intval($postdata['catid']);
			$data['displayorder'] = intval($postdata['displayorder']);
			$data['color'] = $postdata['color'];
			$data['notice'] = '请前往商城购买商品时使用!';
			$data['service_phone'] = '';
			$data['description'] = $postdata['description'];
			$data['use_limit'] = intval($postdata['use_limit']) <= 1 ? 1 : intval($postdata['use_limit']);
			$data['get_limit'] = intval($postdata['get_limit']) <= 1 ? 1 : intval($postdata['get_limit']);
			$data['can_share'] = $postdata['can_share'] == 'on' ? 1 : 0;
			$data['can_give_friend'] = $postdata['can_give_friend'] == 'on' ? 1 : 0;
			$data['center_title'] = $postdata['center_title'];
			$data['center_sub_title'] = $postdata['center_sub_title'];
			$data['center_url'] = $postdata['center_url'];
			$data['setcustom'] = intval($postdata['setcustom']);
			$data['custom_url_name'] = $postdata['custom_url_name'];
			$data['custom_url_sub_title'] = $postdata['custom_url_sub_title'];
			$data['custom_url'] = $postdata['custom_url'];
			$data['setpromotion'] = '1';
			$data['promotion_url_name'] = $postdata['promotion_url_name'];
			$data['promotion_url_sub_title'] = $postdata['promotion_url_sub_title'];
			$data['promotion_url'] = $postdata['promotion_url'];
			$data['limitdiscounttype'] = intval($postdata['limitdiscounttype']);
			$limitgoodcatetype = intval($postdata['limitgoodcatetype']);
			$limitgoodtype = intval($postdata['limitgoodtype']);
			$data['limitgoodcatetype'] = $limitgoodcatetype;
			$data['limitgoodtype'] = $limitgoodtype;
			$data['limitgoodcateids'] = $postdata['limitgoodcateids'];
			$data['limitgoodids'] =$postdata['limitgoodids'];
			$data['gettype'] =$postdata['gettype'];
			$data['gettype'] = 1;
			$data['tagtitle'] = $postdata['tagtitle'];
			$data['settitlecolor'] = intval($postdata['settitlecolor']);
			$data['titlecolor'] = $postdata['titlecolor'];
			$islimitlevel = 0;
			$data['islimitlevel'] = $islimitlevel;
			$data['limitmemberlevels'] = '';
			$data['limitagentlevels'] = '';
			$data['limitpartnerlevels'] = '';
			$data['limitaagentlevels'] = '';
			
			
			if (!empty($item)) {
				
					$imgurl = $postdata['logo_url'];

				

					$result = $this->wxcard__updateimg($imgurl);

					 if($result['errmsg'] && $result['errmsg']!='ok') {
						exi('上传的logo图片限制文件大小限制1MB，像素为300*300，仅支持JPG、PNG格式。','error');
					} 

					$data['logo_url'] = $postdata['logo_url'];
					$data['wxlogourl'] = $result['url'];
				

				if ($postdata['datetype'] == '1') {
					$data['datetype'] = $postdata['datetype'];
					$begin_timestamp = $postdata['timestamp']['tstart'];
					$end_timestamp = $postdata['timestamp']['tend'];
					
					$data['begin_timestamp'] = $begin_timestamp;
					$data['end_timestamp'] = $end_timestamp;
				}	
				$result = $this->wxcard__updatecard($data);
				pdo_update('jy_weishop_wxcard', $data, array('id' => $id, 'uniacid' => $_SESSION['uniacid']));
				
			}
			else {	
				$data['card_type'] = 'CASH';
				if (empty($postdata['logo_url'])) {
					exi('logo图片未上传','error');
				}

				$imgurl = $postdata['logo_url'];

				//print_r($imgurl);exit();

				$result =$this->wxcard__updateimg($imgurl);
				if($result['errmsg'] && $result['errmsg']!='ok') {
						exi('上传的logo图片限制文件大小限制1MB，像素为300*300，仅支持JPG、PNG格式。','error');
					} 
					
				if($postdata['abstractimglocalpath']){
					$result2 =$this->wxcard__updateimg($postdata['abstractimglocalpath']);
					if($result2['errmsg'] && $result2['errmsg']!='ok') {
							exi('上传的logo图片限制文件大小限制1MB，像素为300*300，仅支持JPG、PNG格式。','error');
						} 
				$data['abstract']=$postdata['abstract'];	
				$data['abstractimg'] = $postdata['abstractimglocalpath'];
				$data['icon_url_list'] = $result2['url'];
				}
				
				
				$data['logo_url'] = $postdata['logo_url'];
				$data['wxlogourl'] = $result['url'];
				$data['brand_name'] = $postdata['brand_name'];
				$data['title'] = $postdata['title'];
				$data['datetype'] = $postdata['datetype'];

				if ($postdata['datetype'] == '1') {
					$begin_timestamp = $postdata['timestamp']['tstart'];
					$end_timestamp = $postdata['timestamp']['tend'];					
					$data['begin_timestamp'] = $begin_timestamp;
					$data['end_timestamp'] = $end_timestamp;
				}
				else if ($postdata['datetype'] == '0') {
					$data['fixed_term'] = intval($postdata['fixed_term']);
					$data['fixed_begin_term'] = intval($postdata['fixed_begin_term']);
				}
				else {
					exi( '请选择有效期类型!','error');
				}

				$data['quantity'] = intval($postdata['total_quantity']) <= 1 ? 1 : intval($postdata['total_quantity']);
				$data['total_quantity'] = intval($postdata['total_quantity']) <= 1 ? 1 : intval($postdata['total_quantity']);
				$data['can_use_with_other_discount'] = intval($postdata['can_use_with_other_discount']);
				$data['setabstract'] = intval($postdata['setabstract']);

			/*	if (!empty($postdata['setabstract'])) {
					if (empty($postdata['abstractimglocalpath'])) {
						show_json(0, '封面图片未上传');
					}

					$data['abstract'] = $postdata['abstract'];
					$imgurl = tomeida($postdata['abstractimglocalpath']);

					

					$result =$this->updateimg($imgurl);


					$data['abstractimg'] = $postdata['abstractimgurl'];
					$data['icon_url_list'] = $result['url'];
				} */

			
					$data['use_condition'] = $postdata['use_condition'] == 'on' ? 1 : 0;
					$data['accept_category'] = $postdata['accept_category'];
					$data['reject_category'] = $postdata['reject_category'];
					$data['least_cost'] = intval(floatval($postdata['least_cost']) * 100);
					$data['reduce_cost'] = intval(floatval($postdata['reduce_cost']) * 100);
					if ((10000000000 < $data['reduce_cost']) || ($data['reduce_cost'] <= 0)) {
						exi( '您好,您输入的金额不正确!','error');
					}			
				$result = $this->wxcard__createcard($data);
				 if($result['errmsg'] && $result['errmsg']!='ok') {
						exi('卡券创建失败','error');
				} 
				$data['card_id'] = $result['card_id'];
				pdo_insert('jy_weishop_wxcard', $data);
				exi('添加成功','','cash');
			}
}
//折扣券
if($_RQ['op']=='discount'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid'],'card_type'=>'DISCOUNT');
    $orderby='id desc';
	$total='';
	
	$return['list']=pdo_getslice('jy_weishop_wxcard',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$return['type']=array('CASH'=>'代金券','DISCOUNT'=>'折扣券');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'cash','title'=>'代金券'),
			array('op'=>'discount','title'=>'折扣券'),
	
		),
		'list'=>array('data'=>'list','edit_op'=>'editdiscount','list'=>array(
			array('name'=>'title','title'=>'卡券名称','type'=>'string'),
			array('name'=>'card_type','title'=>'卡券类型','type'=>'radio','options'=>'type'),
			array('name'=>'total_quantity','title'=>'总库存','type'=>'string'),
			array('name'=>'quantity','title'=>'当前库存','type'=>'string'),				
		)),
		'search_one'=>array('name'=>'title','add_op'=>'adddiscount'),
	);
	$return['htmls']=$htmls;
	
	exi($return);
}
if($_RQ['op']=='adddiscount'){
	$id=intval($_RQ['id']);
	if($id){
	$item=pdo_get('jy_weishop_wxcard',array('id'=>$id));
	}else{
		$item=array();
	}
    $return['item']=$item;
	$return['group']=pdo_getall('jy_weishop_coupon_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	
	$return['isdescsys']=array('0'=>'不使用','1'=>'使用');
	$return['gettype']=array('1'=>'显示','0'=>'不显示');
	$return['limitdiscounttype']=array('0'=>'无优惠使用限制','1'=>'不可与促销优惠同时使用','2'=>'不可与会员折扣同时使用','3'=>'不可与促销优惠和会员折扣同时使用');
	$return['limitgoodtype']=array('0'=>'不添加商品限制','1'=>'允许以下商品使用');
	$return['limitgoodcatetype']=array('0'=>'不添加商品分类限制','1'=>'允许以下商品分类使用');
	$return['coupontype']=array('0'=>'返余额','1'=>'返积分','2'=>'返现金');
	$return['timelimittype']=array('0'=>'获得多少天内','1'=>'时间段内');
	$htmls=array(
		'type'=>'edit',
		'post'=>'post2',
		'tabs'=>array(
			array('op'=>'cash','title'=>'代金券'),
			array('op'=>'discount','title'=>'折扣券'),
		),
		'desc'=>'折扣券设置',
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本设置',
				'list'=>array(
					array('type'=>'string','name'=>'displayorder','title'=>'排序','help'=>'越大越靠前'),
					array('type'=>'string','name'=>'title','title'=>'卡券名称','help'=>'卡券标题不能超过9个字符,此参数一旦创建则无法修改'),
					array('type'=>'select','name'=>'catid','title'=>'分类','options'=>'group'),
					array('type'=>'image','name'=>'logo_url','title'=>'logo图片','help'=>'卡券的商户logo，建议像素为300*300。图片大小不能超过1mb,仅支持jpg,png格式'),
					array('type'=>'string','name'=>'brand_name','title'=>'商铺名称','help'=>'商铺名称限制在12个字符之内,此参数一旦创建则无法修改'),
					
					array('type'=>'string','name'=>'total_quantity','title'=>'库存总量','help'=>'总共发布的库存总量，必须大于1'),

					array('type'=>'string','name'=>'center_title','title'=>'卡券顶部按钮文字','help'=>'例如：立即使用(不能超过5个字符)'),
					array('type'=>'string','name'=>'center_sub_title','title'=>'卡券顶部按钮引导语','help'=>'(不能超过6个字符)'),
					array('type'=>'string','name'=>'center_url','title'=>'卡券顶部按钮链接','help'=>'仅卡券被用户领取且处于有效状态时显示（未到有效期、转赠中、核销后不显示）。'),
					array('type'=>'image','name'=>'abstractimglocalpath','title'=>'封面图片','help'=>'图片建议尺寸：850像素*350像素，大小不超过2M。'),
					array('type'=>'string','name'=>'abstract','title'=>'封面简介','help'=>'封面一旦创建完成则不允许修改,请注意!'),
					array('type'=>'string','name'=>'discount','title'=>'折扣额度','help'=>'请填写1-9.9之间的数字，精确到小数点后1位,此参数一旦创建则无法修改'),
					
					array('type'=>'radio','name'=>'datetype','title'=>'使用时间限制','options'=>'timelimittype'),
					array('type'=>'string','name'=>'fixed_term','title'=>'在领取后天数内有效','show'=>array('datetype'=>0)),
					array('type'=>'time_range','name'=>'timestamp','title'=>'在时间段内有效','show'=>array('datetype'=>1)),	
					array('type'=>'string','name'=>'get_limit','title'=>'领券限制','help'=>'每个用户领券上限，必须大于1如不填，则默认为1'),
					array('type'=>'string','name'=>'use_limit','title'=>'使用限制','help'=>'每个用户可以使用此卡券的次数,必须大于1如不填，则默认为1'),
					array('type'=>'text','name'=>'description','title'=>'使用须知'),
		)
	),
			array(
				'title'=>'领取设置',
				'list'=>array(
					array('type'=>'radio','name'=>'gettype','options'=>'gettype','title'=>'加入领券中心','help'=>'越大越靠前'),
							
				)
			),
			array(
				'title'=>'使用限制',
				'list'=>array(
					array('type'=>'radio','name'=>'limitdiscounttype','options'=>'limitdiscounttype','title'=>'优惠使用限制'),
					array('type'=>'radio','name'=>'limitgoodcatetype','options'=>'limitgoodcatetype','title'=>'商品分类使用限制'),	
					array('type'=>'checkbox','name'=>'limitgoodcateids','title'=>'选择商品分类','show'=>array('limitgoodcatetype'=>1)),
					array('type'=>'radio','name'=>'limitgoodtype','options'=>'limitgoodtype','title'=>'商品使用限制'),
					array('type'=>'checkbox','name'=>'limitgoodids','title'=>'选择商品','show'=>array('limitgoodtype'=>1)),
								
				)
			),
	)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post2'){
			$id=intval($_RQ['id']);
			if($id){
			$item=pdo_get('jy_weishop_wxcard',array('id'=>$id));
			}else{
				$item=array();
			}
			$postdata=get_postdata();
			//print_r($postdata);exit();
			$data = array();
			$data['uniacid'] = $_SESSION['uniacid'];
			$data['catid'] = intval($postdata['catid']);
			$data['displayorder'] = intval($postdata['displayorder']);
			$data['color'] = $postdata['color'];
			$data['notice'] = '请前往商城购买商品时使用!';
			$data['service_phone'] = '';
			$data['description'] = $postdata['description'];
			$data['use_limit'] = intval($postdata['use_limit']) <= 1 ? 1 : intval($postdata['use_limit']);
			$data['get_limit'] = intval($postdata['get_limit']) <= 1 ? 1 : intval($postdata['get_limit']);
			$data['can_share'] = $postdata['can_share'] == 'on' ? 1 : 0;
			$data['can_give_friend'] = $postdata['can_give_friend'] == 'on' ? 1 : 0;
			$data['center_title'] = $postdata['center_title'];
			$data['center_sub_title'] = $postdata['center_sub_title'];
			$data['center_url'] = $postdata['center_url'];
			$data['setcustom'] = intval($postdata['setcustom']);
			$data['custom_url_name'] = $postdata['custom_url_name'];
			$data['custom_url_sub_title'] = $postdata['custom_url_sub_title'];
			$data['custom_url'] = $postdata['custom_url'];
			$data['setpromotion'] = '1';
			$data['promotion_url_name'] = $postdata['promotion_url_name'];
			$data['promotion_url_sub_title'] = $postdata['promotion_url_sub_title'];
			$data['promotion_url'] = $postdata['promotion_url'];
			$data['limitdiscounttype'] = intval($postdata['limitdiscounttype']);
			$limitgoodcatetype = intval($postdata['limitgoodcatetype']);
			$limitgoodtype = intval($postdata['limitgoodtype']);
			$data['limitgoodcatetype'] = $limitgoodcatetype;
			$data['limitgoodtype'] = $limitgoodtype;
			$data['limitgoodcateids'] = $postdata['limitgoodcateids'];
			$data['limitgoodids'] =$postdata['limitgoodids'];
			$data['gettype'] =$postdata['gettype'];
			$data['gettype'] = 1;
			$data['tagtitle'] = $postdata['tagtitle'];
			$data['settitlecolor'] = intval($postdata['settitlecolor']);
			$data['titlecolor'] = $postdata['titlecolor'];
			$islimitlevel = 0;
			$data['islimitlevel'] = $islimitlevel;
			$data['limitmemberlevels'] = '';
			$data['limitagentlevels'] = '';
			$data['limitpartnerlevels'] = '';
			$data['limitaagentlevels'] = '';
			
			
			if (!empty($item)) {
				
					$imgurl = tomedia($postdata['logo_url']);

				

					$result = $this->wxcard__updateimg($imgurl);

					 if($result['errmsg'] && $result['errmsg']!='ok') {
						exi('上传的logo图片限制文件大小限制1MB，像素为300*300，仅支持JPG、PNG格式。','error');
					} 

					$data['logo_url'] = $postdata['logo_url'];
					$data['wxlogourl'] = $result['url'];
				

				if ($postdata['datetype'] == '1') {
					$data['datetype'] = $postdata['datetype'];
					$begin_timestamp = $postdata['timestamp']['tstart'];
					$end_timestamp = $postdata['timestamp']['tend'];
					
					$data['begin_timestamp'] = $begin_timestamp;
					$data['end_timestamp'] = $end_timestamp;
				}

				
				$result = $this->wxcard__updatecard($data);


				pdo_update('jy_weishop_wxcard', $data, array('id' => $id, 'uniacid' => $_SESSION['uniacid']));
				
			}
			else {
				
				$data['card_type'] = 'DISCOUNT';
				

				if (empty($postdata['logo_url'])) {
					exi('logo图片未上传','error');
				}

				$imgurl = $postdata['logo_url'];

				//print_r($imgurl);exit();

				$result =$this->wxcard__updateimg($imgurl);
				if($result['errmsg'] && $result['errmsg']!='ok') {
						exi('上传的logo图片限制文件大小限制1MB，像素为300*300，仅支持JPG、PNG格式。','error');
					} 
					
				if($postdata['abstractimglocalpath']){
					$result2 =$this->wxcard__updateimg($postdata['abstractimglocalpath']);
					if($result2['errmsg'] && $result2['errmsg']!='ok') {
							exi('上传的logo图片限制文件大小限制1MB，像素为300*300，仅支持JPG、PNG格式。','error');
						} 
				$data['abstract']=$postdata['abstract'];	
				$data['abstractimg'] = $postdata['abstractimglocalpath'];
				$data['icon_url_list'] = $result2['url'];
				}
				
				
				$data['logo_url'] = $postdata['logo_url'];
				$data['wxlogourl'] = $result['url'];
				$data['brand_name'] = $postdata['brand_name'];
				$data['title'] = $postdata['title'];
				$data['datetype'] = $postdata['datetype'];

				if ($postdata['datetype'] == '1') {
					$begin_timestamp = $postdata['timestamp']['tstart'];
					$end_timestamp = $postdata['timestamp']['tend'];					
					$data['begin_timestamp'] = $begin_timestamp;
					$data['end_timestamp'] = $end_timestamp;
				}
				else if ($postdata['datetype'] == '0') {
					$data['fixed_term'] = intval($postdata['fixed_term']);
					$data['fixed_begin_term'] = intval($postdata['fixed_begin_term']);
				}
				else {
					exi( '请选择有效期类型!','error');
				}

				$data['quantity'] = intval($postdata['total_quantity']) <= 1 ? 1 : intval($postdata['total_quantity']);
				$data['total_quantity'] = intval($postdata['total_quantity']) <= 1 ? 1 : intval($postdata['total_quantity']);
				$data['can_use_with_other_discount'] = intval($postdata['can_use_with_other_discount']);
				$data['setabstract'] = intval($postdata['setabstract']);

			/*	if (!empty($postdata['setabstract'])) {
					if (empty($postdata['abstractimglocalpath'])) {
						show_json(0, '封面图片未上传');
					}

					$data['abstract'] = $postdata['abstract'];
					$imgurl = tomeida($postdata['abstractimglocalpath']);

					

					$result =$this->updateimg($imgurl);


					$data['abstractimg'] = $postdata['abstractimgurl'];
					$data['icon_url_list'] = $result['url'];
				} */

			
					$discount = floatval($postdata['discount']);
					$data['discount'] = $discount;
					
					if ((10 <= $discount) || ($discount < 1)) {
						exi( '您好,您输入的折扣范围不对! 请输入 1 ~ 9.9 之间数','error');
					}

					$discount = (10 - $discount) * 10;
					$data['discount'] = $discount;
				

				$result = $this->wxcard__createcard($data);

				 if($result['errmsg'] && $result['errmsg']!='ok') {
						exi('卡券创建失败','error');
				} 

				$data['card_id'] = $result['card_id'];
				pdo_insert('jy_weishop_wxcard', $data);
				exi('添加成功','','cash');
			}
}
if($_RQ['op']=='editdiscount'){
	$id=intval($_RQ['id']);
	if($id){
	$item=pdo_get('jy_weishop_wxcard',array('id'=>$id));
	$item['timestamp']=array('tstart'=>$item['begin_timestamp'],'tend'=>$item['end_timestamp']);
	}else{
		$item=array();
	}
    $return['item']=$item;
	$return['group']=pdo_getall('jy_weishop_coupon_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	
	$return['isdescsys']=array('0'=>'不使用','1'=>'使用');
	$return['gettype']=array('1'=>'显示','0'=>'不显示');
	$return['limitdiscounttype']=array('0'=>'无优惠使用限制','1'=>'不可与促销优惠同时使用','2'=>'不可与会员折扣同时使用','3'=>'不可与促销优惠和会员折扣同时使用');
	$return['limitgoodtype']=array('0'=>'不添加商品限制','1'=>'允许以下商品使用');
	$return['limitgoodcatetype']=array('0'=>'不添加商品分类限制','1'=>'允许以下商品分类使用');
	$return['coupontype']=array('0'=>'返余额','1'=>'返积分','2'=>'返现金');
	$return['timelimittype']=array('0'=>'获得多少天内','1'=>'时间段内');
	$htmls=array(
		'type'=>'edit',
		'post'=>'discountpost',
		'tabs'=>array(
			array('op'=>'cash','title'=>'代金券'),
			array('op'=>'discount','title'=>'折扣券'),
		),
		'desc'=>'优惠券设置',
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本设置',
				'list'=>array(
					array('type'=>'string','name'=>'displayorder','title'=>'排序','help'=>'越大越靠前'),
					array('type'=>'string','name'=>'title','title'=>'卡券名称','help'=>'卡券标题不能超过9个字符,此参数一旦创建则无法修改','read'=>true),
					array('type'=>'select','name'=>'catid','title'=>'分类','options'=>'group'),
					array('type'=>'image','name'=>'logo_url','title'=>'logo图片','help'=>'卡券的商户logo，建议像素为300*300。图片大小不能超过1mb,仅支持jpg,png格式'),
					array('type'=>'string','name'=>'brand_name','title'=>'商铺名称','help'=>'商铺名称限制在12个字符之内,此参数一旦创建则无法修改','read'=>true),
					
					array('type'=>'string','name'=>'total_quantity','title'=>'库存总量','help'=>'总共发布的库存总量，必须大于1','read'=>true),

					array('type'=>'string','name'=>'center_title','title'=>'卡券顶部按钮文字','help'=>'例如：立即使用(不能超过5个字符)'),
					array('type'=>'string','name'=>'center_sub_title','title'=>'卡券顶部按钮引导语','help'=>'(不能超过6个字符)'),
					array('type'=>'string','name'=>'center_url','title'=>'卡券顶部按钮链接','help'=>'仅卡券被用户领取且处于有效状态时显示（未到有效期、转赠中、核销后不显示）。'),
					array('type'=>'image','name'=>'abstractimglocalpath','title'=>'封面图片','help'=>'图片建议尺寸：850像素*350像素，大小不超过2M。','read'=>true),
					array('type'=>'string','name'=>'abstract','title'=>'封面简介','help'=>'封面一旦创建完成则不允许修改,请注意!','read'=>true),
					array('type'=>'string','name'=>'least_cost','title'=>'使用条件','help'=>'消费满多少可用, 空或0 不限制,此参数一旦创建则无法修改','read'=>true),
					array('type'=>'string','name'=>'discount','title'=>'折扣额度','help'=>'请填写1-9.9之间的数字，精确到小数点后1位,此参数一旦创建则无法修改','read'=>true),
					array('type'=>'radio','name'=>'datetype','title'=>'使用时间限制','options'=>'timelimittype'),
					array('type'=>'string','name'=>'fixed_term','title'=>'在领取后天数内有效','show'=>array('datetype'=>0)),
					array('type'=>'time_range','name'=>'timestamp','title'=>'在时间段内有效','show'=>array('datetype'=>1)),	
					array('type'=>'string','name'=>'get_limit','title'=>'领券限制','help'=>'每个用户领券上限，必须大于1如不填，则默认为1'),
					array('type'=>'string','name'=>'use_limit','title'=>'使用限制','help'=>'每个用户可以使用此卡券的次数,必须大于1如不填，则默认为1'),
					array('type'=>'text','name'=>'description','title'=>'使用须知'),
		)
	),
			array(
				'title'=>'领取设置',
				'list'=>array(
					array('type'=>'radio','name'=>'gettype','options'=>'gettype','title'=>'加入领券中心','help'=>'越大越靠前'),
							
				)
			),
			array(
				'title'=>'使用限制',
				'list'=>array(
					array('type'=>'radio','name'=>'limitdiscounttype','options'=>'limitdiscounttype','title'=>'优惠使用限制'),
					array('type'=>'radio','name'=>'limitgoodcatetype','options'=>'limitgoodcatetype','title'=>'商品分类使用限制'),	
					array('type'=>'checkbox','name'=>'limitgoodcateids','title'=>'选择商品分类','show'=>array('limitgoodcatetype'=>1)),
					array('type'=>'radio','name'=>'limitgoodtype','options'=>'limitgoodtype','title'=>'商品使用限制'),
					array('type'=>'checkbox','name'=>'limitgoodids','title'=>'选择商品','show'=>array('limitgoodtype'=>1)),
								
				)
			),
	)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='discountpost'){
			$postdata=get_postdata();
			$id=intval($postdata['id']);
			
			$item=pdo_get('jy_weishop_wxcard',array('id'=>$id));
			$data = array();
			$data['card_id']=$item['card_id'];
			$data['catid'] = intval($postdata['catid']);
			$data['displayorder'] = intval($postdata['displayorder']);
			$data['card_type']='DISCOUNT';
			$data['description'] = $postdata['description'];
			$data['use_limit'] = intval($postdata['use_limit']) <= 1 ? 1 : intval($postdata['use_limit']);
			$data['get_limit'] = intval($postdata['get_limit']) <= 1 ? 1 : intval($postdata['get_limit']);
			$data['gettype'] =$postdata['gettype'];
			$data['center_title'] = $postdata['center_title'];
			$data['center_sub_title'] = $postdata['center_sub_title'];
			$data['center_url'] = $postdata['center_url'];
			$data['limitdiscounttype'] = $postdata['limitdiscounttype'];
			$limitgoodcatetype = intval($postdata['limitgoodcatetype']);
			$limitgoodtype = intval($postdata['limitgoodtype']);
			$data['limitgoodcatetype'] = $limitgoodcatetype;
			$data['limitgoodtype'] = $limitgoodtype;
			$data['limitgoodcateids'] = $postdata['limitgoodcateids'];
			$data['limitgoodids'] =$postdata['limitgoodids'];
			
					$imgurl = $postdata['logo_url'];
					$result = $this->wxcard__updateimg($imgurl);

					 if($result['errmsg'] && $result['errmsg']!='ok') {
						exi('上传的logo图片限制文件大小限制1MB，像素为300*300，仅支持JPG、PNG格式。','error');
					} 

					$data['logo_url'] = $postdata['logo_url'];
					$data['wxlogourl'] = $result['url'];
				

				if ($postdata['datetype'] == '1') {
					$data['datetype'] = $postdata['datetype'];
					$begin_timestamp = $postdata['timestamp']['tstart'];
					$end_timestamp = $postdata['timestamp']['tend'];
					
					$data['begin_timestamp'] = $begin_timestamp;
					$data['end_timestamp'] = $end_timestamp;
				}

				
				$result = $this->wxcard__updatecard($data);	
				if($result['errmsg'] && $result['errmsg']!='ok') {
						exi('卡券更新失败','error');
				} 
				pdo_update('jy_weishop_wxcard', $data, array('id' => $id, 'uniacid' => $_SESSION['uniacid']));
				exi('卡券更新成功','','discount');
			
			
}

if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		$card_id=pdo_getcolumn('jy_weishop_wxcard',array('id'=>$id),'card_id');
		$res=$this->wxcard__carddelete($card_id);
		if($res['errmsg'] && $res['errmsg']!='ok') {
			exi('删除失败','error');
		} 
		pdo_delete('jy_weishop_wxcard',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_weishop_wxcard',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}

?>